<?php

namespace app\tripapi\model;

use think\Model;

use think\Db;

class TravelApplyCustomer extends Model{

    public function TravelApplyCustomerSave($id,$params){
        $time = time();
        $travel_apply_customer_list = $params['number'];
        foreach($travel_apply_customer_list as $key => $value){
            // 转换时间跨度为起止时间 和 结束时间
            $start_time = $params['Time'][$key];
            $final = explode(' - ', $start_time);
            $travel_apply_customer['apply_id'] = $id; // 出差申请单id 上述数据存储之后生成
            $travel_apply_customer['item_id'] = $params['item_id'][$key]; // 月度申请单号
            $travel_apply_customer['start_time'] = $final['0']; // 开始日期
            $travel_apply_customer['end_time'] = $final['1']; // 结束日期
            $travel_apply_customer['trip_plan'] = iconv('UTF-8', 'gbk', $params['trip_plan'][$key]); // 行程计划
            $travel_apply_customer['live_place'] = iconv('UTF-8', 'gbk', $params['live_place'][$key]); // 住宿地址
            $travel_apply_customer['customer_name'] = iconv('UTF-8', 'gbk', $params['customer_name'][$key]); // 客户名称
            $travel_apply_customer['travel_status'] = "publish"; // 创建人id
            $travel_apply_customer['creator'] = iconv('UTF-8', 'gbk', $params['creator']); // 创建人
            $travel_apply_customer['creator_id'] = $params['creator_id']; // 创建人id
            $travel_apply_customer['create_time'] = $time; // 创建日期
            // 写入 travel apply customer 数据表
//            exit;
            Db::name('travel_apply_customer')->insert($travel_apply_customer);
        }
        return true;
    }

    // 获取客户明细
    public function GetCustomerItemById($id){
        $result = Db::name('travel_apply_customer')->where([
            'apply_id'=>['=',$id],
            'travel_status'=>['=','publish']
        ])->select();
        foreach($result as $key => $value){
            $result[$key]['trip_plan'] = iconv('GBK','UTF-8', $value['trip_plan']);
            $result[$key]['live_place'] = iconv('GBK','UTF-8', $value['live_place']);
            $result[$key]['customer_name'] = iconv('GBK','UTF-8', $value['customer_name']);
            $result[$key]['creator'] = iconv('GBK','UTF-8', $value['creator']);
        }
        return $result;
    }
    /*
     * 删除
     */
    public function ApplyCustomerDelete($id){
        $result = Db::name('travel_apply_customer')->where([
            'apply_id'=>['=',$id],
            'travel_status'=>['=','publish']
        ])->delete();
        return $result;
    }
    /*
     * 更新
     */
    public function ApplyCustomerUpdate($id, $params){
//        print_r($params);exit;
        $data['travel_status'] = "modify";
        $result = Db::name('travel_apply_customer')->where([
            'apply_id'=>['=',$id],
            'travel_status'=>['=','publish']
        ])->update($data);
//        if($result){
            $time = time();
            $travel_apply_customer_list = $params['number'];
            foreach($travel_apply_customer_list as $key => $value){
                // 转换时间跨度为起止时间 和 结束时间
                $start_time = $params['Time'][$key];
                $final = explode(' - ', $start_time);
                $travel_apply_customer['apply_id'] = $id; // 出差申请单id 上述数据存储之后生成
                $travel_apply_customer['item_id'] = $params['item_id'][$key]; // 月度申请单号
                $travel_apply_customer['start_time'] = $final['0']; // 开始日期
                $travel_apply_customer['end_time'] = $final['1']; // 结束日期
                $travel_apply_customer['trip_plan'] = iconv('UTF-8', 'gbk', $params['trip_plan'][$key]); // 行程计划
                $travel_apply_customer['live_place'] = iconv('UTF-8', 'gbk', $params['live_place'][$key]); // 住宿地址
                $travel_apply_customer['customer_name'] = iconv('UTF-8', 'gbk', $params['customer_name'][$key]); // 客户名称
                $travel_apply_customer['travel_status'] = "publish"; // 创建人id
                $travel_apply_customer['creator'] = iconv('UTF-8', 'gbk', $params['creator']); // 创建人
                $travel_apply_customer['creator_id'] = $params['creator_id']; // 创建人id
                $travel_apply_customer['create_time'] = $time; // 创建日期
                // 写入 travel apply customer 数据表
                Db::name('travel_apply_customer')->insert($travel_apply_customer);
            }
//            return $result;
//        }else{
//            return false;
//        }
    }
}

?>